<template>
	<view class="wap">
		<view class="head">
			<view class="head-login flex-x-center flex-y-center">
				<image src="https://video.file.zhuochengyun.com/bwc/uploads/20240207/20240207091947780300.png">
				</image>
			</view>
			<view class="content">
				<view>申请获取以下权限</view>
				<view>获得你的公开信息(昵称，头像等)</view>
			</view>
			<view class="bottom">
				<button @click="login()" class="" type="default">授权登录</button>
			</view>

		</view>
		<fWxLogin ref="fwloginRef" :showWxLogin="showWxLogin" @setUserInfoCallback="handleLoginClick"
			@onClose="showWxLogin = false">
		</fWxLogin>
	</view>
</template>


<script>
	import fWxLogin from '@/components/f_wxLogin.vue'
	import {
		wxLogin,
		appid
	} from '../../apirequest/api.js';
	export default {
		components: {
			fWxLogin
		},
		data() {
			return {
				params: {},
				showWxLogin: true,
				loginCode: ''
			}
		},
		onLoad(option) {
			this.params = option
		},
		onShow() {
			// #ifdef H5
			this.weChatAuth();
			// #endif
		},
		methods: {
			xieyi: function() {
				uni.navigateTo({
					url: '/pages/agreement/agreement'
				})
			},
			login: function() {
				// #ifdef MP-WEIXIN

				this.wxLogin()
				// #endif
				// #ifdef H5
				this.h5Login()
				// #endif
			},
			handleLoginClick(userInfo) {
				this.getUserInfo(userInfo)
			},
			getUserInfo(userInfo) {
				uni.showLoading()
				// nickName: userInfo.nick_name,
				// avatarUrl: userInfo.avatar_url,
				const that = this
				const parent_id = uni.getStorageSync('parent_id')
				 
				wxLogin({
					code: this.loginCode,
					userInfo: {
						nickName: userInfo.nick_name,
						avatarUrl: userInfo.avatar_url,
					},
					parent_id: parent_id || '0'
				}).then(res => {
					uni.hideLoading()
					const openid = res.data.userinfo.openid;
					that.openid = openid
					if (res.code == 1) {

						uni.setStorageSync('openid', res.data.userinfo
							.openid);
						uni.setStorageSync('userInfo', res.data.userinfo);
						uni.showToast({
							title: res.msg,
							icon: 'success'
						});
						setTimeout(() => {
							if (res.data.userinfo.mobile) {
								uni.reLaunch({
									url: '/pages/changePhone/changePhone'
								})
							} else {

								uni.reLaunch({
									url: '/pages/index/newIndex',
								});
							}
						}, 1000);
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'success'
						});
					}
					return
				}).catch(() => {
					uni.hideLoading()
				})

			},
			wxLogin() {
				// 在需要获取用户信息的页面中调用以下代码
				uni.showLoading({
					mask: true
				})
				var that = this
				uni.getUserProfile({
					desc: '用户信息授权',
					success: (res) => {
						const userInfo = res.userInfo;
						that.wxApi_userinfo = userInfo

						uni.login({
							success: function(result) {
								if (result.code) {
									that.loginCode = result.code
									uni.hideLoading()
									that.$refs.fwloginRef.open('', '')
									// 发起网络请求，换取openid

								} else {
									console.log('登录失败！' + result.errMsg);
									uni.hideLoading()
								}
							},
							fail: function(err) {
								console.log(err);
								uni.hideLoading()
							}
						});
					},
					fail: (res) => {
						console.log(res)
						uni.hideLoading()
					}
				})
			},
			h5Login() {
				// 获取appid
				appid().then(res => {
					let redirectUrl = encodeURIComponent(window.location.href.replace(/[?&]code=(\w+)/i, '')
						.replace(/&state=fanpingsanguo/ig, ''));
					window.location.replace(
						`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${res.data.app_id}&redirect_uri=${redirectUrl}&response_type=code&scope=snsapi_userinfo&state=fanpingsanguo#wechat_redirect`
					);
				});
			},
			getUrlParam(name) {
				var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
				var r = window.location.search.substr(1).match(reg);
				if (r != null) return unescape(r[2]);
				return null;
			},
			// 微信授权
			weChatAuth() {
				let code = this.getUrlParam('code');
				console.log(code)
				let old_code = uni.getStorageSync('old_code');
				if (code && old_code != code) {
					uni.setStorageSync('old_code', code);
					let parent_id = uni.getStorageSync('parent_id');
					let redirect_url = uni.getStorageSync('redirect_url');
					console.log(redirect_url);
					wxLogin({
						code: code,
						parent_id: parent_id || 0,
					}).then(res => {
						if (res.code == 1) {
							uni.setStorageSync('token', res.data.userinfo.token);
							if (!res.data.userinfo.mobile) {
								setTimeout(function() {
									uni.redirectTo({
										url: '/pages/changePhone/changePhone'
									});
								}, 1000);
							} else {
								uni.showToast({
									title: '登录成功',
									icon: 'success'
								});
								setTimeout(function() {
									uni.reLaunch({
										url: '/pages/index/index',
									});
								}, 1000);
							}
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'error'
							});
						}
					});
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.wap .head {
		margin: 9px 14px
	}

	.wap .head .head-login {
		width: 100%;
		height: 192px;
		border-bottom: 1px solid #ccc
	}

	.wap .head .head-login image {
		width: 96px;
		height: 96px;
		border-radius: 9px
	}

	.wap .head .content {
		margin: 38px 0px
	}

	.wap .head .content view:nth-child(1) {
		margin-bottom: 24px
	}

	.wap .head .content view:nth-child(2) {
		color: #9d9d9d
	}

	.wap .head .bottom button {
		color: #fff;
		border: none;
		background-color: #ff8546;
		background-image: linear-gradient(to, right, #f4ac1c, #ffe786);
		border-radius: 29px
	}

	.wap .head .bottom button::after {
		width: 0;
		height: 0
	}

	.wap .head .sign {
		text-align: center
	}

	.wap .head .sign-bottom {
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 9px;
		font-size: 28rpx;
	}

	.wap .head .sign-bottom text {
		font-weight: 700;
		color: #f4ac1c
	}

	.wap .head .sign-bottom .label {
		display: flex;
		align-items: center;
		color: #9d9d9d
	}

	.wap .head .sign-bottom .label .checkbox {
		-webkit-transform: scale(.7);
		transform: scale(.7)
	}
</style>